package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.management.basics.domain.TEmployee;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/passenger/manage")
public class PassengerManageController extends BaseController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITEmployeeService iTEmployeeService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 驾驶员登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/passengerLogin")
    public AjaxResult driverLogin(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.passengerLogin(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/addPassenger")
    public AjaxResult addPassenger(@RequestBody SysUser user)
    {

        String tel = user.getPhonenumber();
        //判断乘客信息是否已在乘客端注册
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getPhonenumber())))
        {
            return AjaxResult.error("注册失败，登录账号已存在");
        }

        Long[] roleIds = new Long[1];
        roleIds[0] = Long.valueOf(101);//初始化角色信息  101 - 乘客
        user.setRoleIds(roleIds);
        user.setUserType("03");//乘客

        user.setDeptId(Long.valueOf(200));//乘客默认的部门信息

        user.setUserName(tel);
        user.setNickName(tel);
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }
}
